package org.ogv.usada.states;

import org.ogv.OGV;
import org.ogv.CAssetManager.AssetDeclaration;
import org.ogv.statemanager.State;
import org.ogv.tween.EaseFactory;
import org.ogv.tween.Tween;
import org.ogv.usada.models.GP;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;

public class ScreenState extends State {
	protected AssetDeclaration 		_assetDecl;
	protected SpriteBatch 			_sb;
	protected OrthographicCamera	_cam;
	protected Vector3				_tempV3;
	protected Music					_bgm;
	
	private Tween	_doorTween;
	private int		_nextState;
	private State	_nextStateS;
	private boolean	_stateChangeReq;
	protected TextureRegion _doorTexR, _doorTexL;
	private Sound _open, _close;
	
	public ScreenState(SpriteBatch sb) {
		_sb 	= sb;
		_tempV3	= new Vector3();
		
		//Init cam
		_cam	= new OrthographicCamera(GP.$().GAME_WIDTH, GP.$().GAME_HEIGHT);
		_cam.position.set(GP.$().GAME_WIDTH >> 1, GP.$().GAME_HEIGHT >> 1, 0);
		_cam.update();
	}
	
	public AssetDeclaration getAssetDecl() {
		return _assetDecl;
	}
	
	protected void updateTouchPoint() {
		_tempV3.set(Gdx.input.getX(), Gdx.input.getY(), 0);
		_cam.unproject(_tempV3);
		_tempV3.y += GP.$().TOUCH_OFFSET_Y;
	}

	protected void initializeDoors() {
		_doorTexR = OGV.asset.getTextureRegion(GP.$().RES_TEXR_DOOR_R);
		_doorTexL = OGV.asset.getTextureRegion(GP.$().RES_TEXR_DOOR_L);
		_doorTween= new Tween();
		_close 	= OGV.asset.get("data/audio/close_door.wav", Sound.class);
		_open 	= OGV.asset.get("data/audio/open_door.wav", Sound.class);
	}

	protected void requestStateChange(int newState) {
		_stateChangeReq = true;
		_nextState = newState;
		startClosingDoor();
	}

	protected void requestStateChange(State newState) {
		_stateChangeReq = true;
		_nextStateS = newState;
		startClosingDoor();
	}

	private void startClosingDoor() {
		_doorTween.init(1, 0, 2f, EaseFactory.OUT_QUAD);
		_open.play(GP.$().SOUND_VOL);
	}
	
	protected void drawDoors() {
		float val = _doorTween.getCurrentValue() * GP.$().GAME_WIDTH;
		_sb.draw(_doorTexR, GP.$().GAME_WIDTH/2 + val, 0);
		_sb.draw(_doorTexL, 0 - val, 0);
	}

	protected void startOpeningDoor() {
		_doorTween.init(0, 1, 2f, EaseFactory.IN_QUAD);
		_stateChangeReq = false;
		_open.play(GP.$().SOUND_VOL);
	}

	protected void updateDoors(float delta) {
		_doorTween.update(delta);
		if(_stateChangeReq && _doorTween.isDone()) {
			if(_nextStateS != null) {
				getStateMgr().requestPushState(_nextStateS);
				_nextStateS = null;
			}
			else if(_nextState < 0) 
				while(_nextState < 0) { getStateMgr().requestPopState(); ++_nextState; }
			else {
				_close.play(GP.$().SOUND_VOL);
				getStateMgr().requestPushState(_nextState);
			}
		}
	}

	protected boolean areDoorsActive() {
		return !_doorTween.isDone();
	}
}
